// pages/person/person.js
const app = getApp()
const wh = require("../../utils/wh.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['审核中', '未通过', '已通过', "已完成"],
    currentTab: 0,
    dataList: [],
    auth_modal: false, // 授权窗口
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    username: '昵称', // 用户信息
    avatar: '../../images/head.png',
    page: 1,
    page_size: 5,
    no_data: 0,
    onearth: false, // 是否已经到达底部
    currentTab_back: 0
  },
  //响应点击导航栏
  navbarTap: function (e) {
    var that = this;
    this.setData({
      currentTab: e.currentTarget.dataset.idx,
      no_data: 0,
      page: 1,
      page_size: 5,
      dataList: [],
      onearth: false
    });
    this.loop();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfoStr = wx.getStorageSync('W_Userinfo');
    if (userInfoStr != '' && userInfoStr != undefined) {
      let userInfo = JSON.parse(wx.getStorageSync('W_Userinfo'));
      if (userInfo != '') {
        this.setData({
          'username': userInfo.nickname,
          'avatar': userInfo.avatar
        })
      }
    }
  },
  loop: function() {
    let postUrl = '/api/madetoorder.loan.user/get_apply_list';
    let idx = this.data.currentTab,
      status_name = 'under_review',
      page = this.data.page,
      page_size = this.data.page_size,
      that = this,
      no_data = this.data.no_data;
    switch (parseInt(idx)) {
      case 0:
        status_name = 'under_review';
        break;
      case 1:
        status_name = 'fail';
        break;
      case 2:
        status_name = 'passed';
        break;
      case 3:
        status_name = 'complete';
        break;
    }
    if (no_data == 0) {
      let postData = {
        type: status_name,
        page: page,
        page_size: page_size
      }
      wh.Post(postUrl, postData, that.apply_list);
    }
  },
  /**
   * 列表
   */
  apply_list: function(data) {
    console.log('数据',data)
    let page_size = this.data.page_size,
      count = data.length,
      page = this.data.page,
      list = this.data.dataList.concat(data);
    if (page_size > count) {
      this.data.no_data = 1;
      if (list.length > 3) {
        this.setData({
          onearth: true
        })
      }
    }
    this.setData({
      dataList: list
    })
  },
  // 取消授权
  cancel_auth: function () {
    wx.switchTab({
      url: '../index/index'
    });
    this.setData({
      auth_modal: false
    })
  },
  /**
   * 确认授权
   */
  bindGetUserInfo: function (e) {
    let userInfo = e.detail, 
    that = this;
    this.setData({ userInfo: userInfo });
    if (e.detail.userInfo) {
      wx.showToast({
        title: '授权成功',
      })
      setTimeout(function () {
        let auth_rs = app.wlogin(userInfo);
        let employId = wx.getStorageSync('employId');
        if (!employId || employId == '') {
          app.employIdCallback = employId => {
            if (employId != '') {
              that.setData({
                auth_modal: false,
                username: userInfo.userInfo.nickName,
                avatar: userInfo.userInfo.avatarUrl
              })
              that.setData({
                currentTab: 0,
                no_data: 0,
                page: 1,
                page_size: 5,
                onearth: false,
                dataList: []
              });
              that.loop();
            }
          }
        }
      }, 1500)
    } else {
        wx.switchTab({
          url: '../index/index'
        })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getExtConfig({
      success: function (res) {
        module.exports.globalData = res.extConfig;
        let s_app_id = wx.getStorageSync('whlw_app_id');
        if (s_app_id != module.exports.globalData.app_id) {
          that.setData({
            auth_modal: true
          })
        } else {
          that.setData({
            currentTab: that.data.currentTab_back,
            no_data: 0,
            page: 1,
            page_size: 5,
            onearth: false,
            dataList: []
          });
          that.loop();
        }
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      currentTab: this.data.currentTab,
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      dataList: []
    });
    this.loop();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;

    if (no_data == 0) {
      this.data.page = page+1;
      this.loop();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})